<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="shell" id="shell">
    <div class="header">
      <h2 class="title">废物の小窝</h2>
      <h3 class="subtitle">Lastyear</h3>
    </div>
    <div class="timeline">
      <div class="item item--active" @click="onclick(item)" :data-text="`《${item}》`" v-for="item, index in timeobj.name"
        :key="index">
        <div class="content">
          <img alt="" :src="imgs[Math.floor(Math.random() * 14) + 1]" class="img">
          <h2 class="content-title">{{ timeobj.time[index] }}</h2>
          <p class="content-desc">
            {{ knownSaying![index] }}
          </p>
        </div>
      </div>


    </div>
  </div>
</template>

<script setup lang="ts">
import { type hitokoto } from '@/types/content'
import axios from 'axios';
import { onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router';
import { imgs } from './ts/img'


const pros = defineProps<{
  timeobj: any
}>()

const imgss = ref<any>([])
//获取名言名录
const knownSaying = ref<string[]>(['', '', '', ''])
Promise.all(pros.timeobj.name.map(() => axios.get<hitokoto>('https://v1.hitokoto.cn/'))).then((res) => {
  console.log(res);
  const datas: string[] = res.map((item) => item.data.hitokoto)
  console.log(datas);

  knownSaying.value = datas
})



const route = useRoute()
const router = useRouter()
imgss.value = imgs
console.log(pros.timeobj);

const onclick = (name: string) => {


  pros.timeobj.name.forEach((item: string, index: number) => {
    console.log(item);
    if (item === name) {
      router.push(`/technology/?type=${route.path.split('/')[2]}&name=${name}_${pros.timeobj.time[index]}`)
    }
  })

}


onMounted(async () => {




  //实现滚动切换显示隐藏卡片
  let items: NodeListOf<HTMLElement> = document.querySelectorAll('.item')
  let i = 0
  let shell: Element | null = null
  shell = document.querySelector('.shell')
  shell?.addEventListener('scroll', function () {
    i = Math.ceil(shell?.scrollTop! / items[2].offsetHeight)
    items.forEach((item, index) => {
      if (i === index) {
        item.classList.add('item--active')
      } else {
        item.classList.remove('item--active')
      }
    })
  })


})





</script>

<style scoped lang="scss">
.shell {
  width: 100%;
  position: relative;
  height: 100vh;
  overflow: auto;
  padding: 80px 0;
  transition: 0.3s ease 0s;
  background-attachment: fixed;
  background-size: cover;
}


.header {
  width: 100%;
  text-align: center;
  margin-bottom: 80px;
  position: relative;
}

.title {
  color: #fff;
  font-size: 46px;
  font-weight: normal;
  margin-top: 30px;
}

.timeline {
  display: flex;
  margin: 0 auto;
  flex-wrap: wrap;
  flex-direction: column;
  max-width: 700px;
  position: relative;
}

.content-title {
  font-weight: normal;
  text-align: center;
  font-size: 55px;
  margin: -10px 0 0 0;
  transition: 0.4s;
  padding: 0 10px;
  box-sizing: border-box;
  color: #fff;
}

.content-desc {
  margin: 0;
  font-size: 15px;
  box-sizing: border-box;
  color: rgba(255, 255, 255, 0.7);
  line-height: 25px;
}

.timeline:before {
  position: absolute;
  left: 50%;
  width: 2px;
  height: 100%;
  margin-left: -1px;
  content: "";
  background: rgba(255, 255, 255, 0.07);
}

.item {
  padding: 40px 0;
  opacity: 0.3;
  filter: blur(2px);
  transition: 0.5s;
  box-sizing: border-box;
  width: calc(50% - 40px);
  display: flex;
  position: relative;
  transform: translateY(-80px);
  margin-top: 50px;
}

.item:before {
  content: attr(data-text);
  letter-spacing: 3px;
  width: 100%;
  position: absolute;
  color: rgb(255, 255, 255);
  font-size: 13px;
  border-left: 2px solid rgba(255, 255, 255, 0.5);
  top: 70%;
  margin-top: -35px;
  padding-left: 15px;
  opacity: 0;
  right: calc(-100% - 56px);
  font: 900 20px '';
  letter-spacing: 5px;
}

.item:nth-child(even) {
  align-self: flex-end;
}

.item:nth-child(even):before {
  right: auto;
  text-align: right;
  left: calc(-100% - 56px);
  padding-left: 0;
  border-left: none;
  border-right: 2px solid rgba(255, 255, 255, 0.5);
  padding-right: 15px;
}

.item--active {
  opacity: 1;
  transform: translateY(0);
  filter: blur(0px);
}

.item--active:before {
  top: 50%;
  transition: 0.3s all 0.2s;
  opacity: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;

}

.item--active .content-title {
  margin: -50px 0 20px 0;
}

.img {
  max-width: 100%;
  box-shadow: 0 10px 15px rgba(0, 0, 0, 0.4);
}

.subtitle {
  color: rgba(255, 255, 255, 0.5);
  font-size: 16px;
  letter-spacing: 5px;
  margin: 10px 0 0 0;
  font-weight: normal;
}

.footer {
  padding: 95px 0;
  text-align: center;
}

.footer a {
  color: #999;
  display: inline-block;
}

@media only screen and (max-width: 767px) {
  .item {
    align-self: baseline !important;
    width: 100%;
    padding: 0 30px 150px 80px;
  }

  /* 时光轴的标题自适应 */
  .item:before {
    padding: 0 !important;
    top: 50px;
    text-align: center !important;
    border: none !important;
    width: 100%;

    content: attr(data-text);
  }

  .item:last-child {
    padding-bottom: 40px;
  }



  /* 时光轴的时间自适应 */
  .content .content-title {
    font-size: 3em !important;
  }
}

@media only screen and (max-width: 767px) {
  .timeline:before {
    left: 40px;
  }
}
</style>